package com.winit.openapi.intelligent.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.intelligent.recommend.command.IntelligentWarehouseReportCommand;
import com.winit.oms.spi.intelligent.recommend.vo.IntelligentWarehouseReport;
import com.winit.oms.spi.outbound.IntelligentWarehouseReportService;
import com.winit.oms.spi.outbound.command.order.QueryReportBySellerCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;

/**
 * 智能推荐接口
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hui.li2 	1.0  		2016年12月11日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/intelligent")
public class IntelligentRecommendController extends BaseController {

    @Resource
    private IntelligentWarehouseReportService intelligentWarehouseReportService;

    /**
     * 查询智能推荐报表集合
     * 
     * @param requestMsg 请求消息
     * @param responseMsg 响应消息
     * @return 请求结果
     */
    @RequestMapping(value = "/getIntelligentReportList")
    public String getIntelligentReportList(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {

        QueryReportBySellerCommand command = new QueryReportBySellerCommand();
        command.setCtx(CommandContext.getContext());
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("winitOrderNo")) {// 订单号
                ValidateUtil.validMaxLength(json, "winitOrderNo", 64);
            }
            if (json.containsKey("sellerOrderNo")) {// 卖家订单号
                ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
            }
            if (json.containsKey("sourceWarehouseCode")) {// 原仓编号
                ValidateUtil.validMaxLength(json, "sourceWarehouseCode", 40);
            }
            if (json.containsKey("orderSource")) {// 订单来源
                ValidateUtil.validMaxLength(json, "orderSource", 10);
            }
            if (json.containsKey("sourceProductCode")) {// 原方案产品编码
                ValidateUtil.validMaxLength(json, "sourceProductCode", 32);
            }
            IntelligentWarehouseReport report = new IntelligentWarehouseReport();
            if (json.containsKey("winitOrderNo")) {// 订单号
                report.setWinitOrderNo(json.getString("winitOrderNo"));
            }
            if (json.containsKey("sellerOrderNo")) {// 卖家订单号
                report.setCustomerOrderNo(json.getString("sellerOrderNo"));
            }
            if (json.containsKey("sourceWarehouseCode")) {// 原仓编号
                report.setSourceWarehouseCode(json.getString("sourceWarehouseCode"));
            }
            if (json.containsKey("sourceProductCode")) {// 原方案产品编码
                report.setSourceProductCode(json.getString("sourceProductCode"));
            }
            if (json.containsKey("orderSource")) {// 订单来源
                report.setOrderSource(json.getString("orderSource"));
            }
            if (json.containsKey("isIntelligent")) {// 是否推荐方案
                report.setType(json.getString("isIntelligent"));
            }
            // 转为时间类型.
            if (json.containsKey("orderDateFrom")) {// 开始时间
                String orderDateStart = json.getString("orderDateFrom");
                orderDateStart += " 00:00:00";
                Date from = DateUtil.parse2Date(orderDateStart, DateUtil.FULL_DATE_STRING);
                report.setStartDate(from);
            }
            if (json.containsKey("orderDateFromTo")) {// 结束时间
                String orderDateEnd = json.getString("orderDateFromTo");
                orderDateEnd += " 23:59:59";
                Date end = DateUtil.parse2Date(orderDateEnd, DateUtil.FULL_DATE_STRING);
                report.setEndDate(end);
            }
            command.setReport(report);
            Searchable<?> searchable = buildSearchablePageParams(json);
            searchable.addSort(new Sort(Direction.DESC, "CREATED"));
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            Searchable<?> searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        Page<IntelligentWarehouseReport> pagelist = intelligentWarehouseReportService.findIntelligentWarehouseReportPage(command);
        if (pagelist != null) {
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", pagelist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 报表详情
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getIntelligentReportDetail")
    public String getIntelligentReportDetail(@ModelAttribute RequestMsg requestMsg,
                                             @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        IntelligentWarehouseReportCommand command = new IntelligentWarehouseReportCommand();
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;
            ValidateUtil.validNotNull(jsonObj, "id");
            ValidateUtil.validMaxLength(jsonObj, "id", 20);
            IntelligentWarehouseReport vo = new IntelligentWarehouseReport();
            vo.setId(jsonObj.getLong("id"));
            command.setIntelligentWarehouseReport(vo);
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        IntelligentWarehouseReport report = intelligentWarehouseReportService.getIntelligentWarehouseReport(command);
        responseMsg.setData(report);
        return SUCCESS;
    }

    protected Map<String, Object> getPageParams(Page pagelist) {

        Map<String, Object> pageMap = new HashMap<String, Object>();
        pageMap.put("pageNo", pagelist.getPageable().getPageNumber());
        pageMap.put("pageSize", pagelist.getPageable().getPageSize());
        pageMap.put("totalCount", pagelist.getTotalElements());

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("pageParams", pageMap);
        return data;
    }

}
